package com.csproj.dispatch.service;

import com.csproj.basic.domain.ElectronicLockMessage;
import com.csproj.basic.repository.ElectronicLockMessageRepository;
import com.csproj.basic.service.dto.ElectronicLockMessageDTO;
import com.csproj.basic.service.mapper.ElectronicLockMessageMapper;
import com.csproj.dispatch.domain.Dispatch;
import com.csproj.dispatch.domain.PositionInfo;
import com.csproj.dispatch.repository.PositionInfoRepository;
import com.csproj.dispatch.service.dto.PositionInfoDTO;
import com.csproj.dispatch.service.mapper.PositionInfoMapper;
import com.csproj.siiat.base.common.dto.BaseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing PositionInfo.
 * 位置信息
 */
@Service
@Transactional
public class PositionInfoService {

    private final Logger log = LoggerFactory.getLogger(PositionInfoService.class);

    private final PositionInfoRepository positionInfoRepository;

    private final PositionInfoMapper positionInfoMapper;

    private final ElectronicLockMessageRepository electronicLockMessageRepository;

    private final ElectronicLockMessageMapper electronicLockMessageMapper;

    public PositionInfoService(PositionInfoRepository positionInfoRepository, PositionInfoMapper positionInfoMapper,
                               ElectronicLockMessageRepository electronicLockMessageRepository,
                               ElectronicLockMessageMapper electronicLockMessageMapper) {
        this.positionInfoRepository = positionInfoRepository;
        this.positionInfoMapper = positionInfoMapper;
        this.electronicLockMessageRepository = electronicLockMessageRepository;
        this.electronicLockMessageMapper = electronicLockMessageMapper;
    }

    /**
     * Save a positionInfo.
     *
     * @param positionInfoDTO the entity to save
     * @return the persisted entity
     */
    public PositionInfoDTO save(PositionInfoDTO positionInfoDTO) {
        log.debug("Request to save PositionInfo : {}", positionInfoDTO);
        PositionInfo positionInfo = positionInfoMapper.toEntity(positionInfoDTO);
        positionInfo = positionInfoRepository.save(positionInfo);
        return positionInfoMapper.toDto(positionInfo);
    }

    /**
     * Get all the positionInfos.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<PositionInfoDTO> findAll() {
        log.debug("Request to get all PositionInfos");
        return positionInfoRepository.findAll().stream()
            .map(positionInfoMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * 上传位置信息
     * @param positionInfoDTO 位置信息
     * @param lockCode 锁编号
     * @return
     */
    public BaseDTO insertPositionInfo(PositionInfoDTO positionInfoDTO, String lockCode){
        if(lockCode==null || "".equals(lockCode)){
            return new BaseDTO(BaseDTO.CODE_PARAM, "lockset_no_can_not_blank", null);
        }
        //TODO 通过锁编号-集装箱编号等查询此次的调度信息
        Dispatch dispatch = new Dispatch();

        //插入位置信息
        PositionInfo positionInfo = positionInfoMapper.toEntity(positionInfoDTO);
        positionInfo.setCreationTime(ZonedDateTime.now());
        positionInfo.setDispatchId(dispatch.getId());
        positionInfo = positionInfoRepository.save(positionInfo);
        return BaseDTO.createBaseDTO(positionInfoMapper.toDto(positionInfo));
    }

    /**
     * 查看实时位置-最新一次位置记录
     * @param dispatchId
     * @return
     */
    public BaseDTO searchPositionInfoNow(String dispatchId){
        if(dispatchId==null || "".equals(dispatchId)){
            return new BaseDTO(BaseDTO.CODE_PARAM, "dipatch_info_id_not_null", null);
        }
    //    PositionInfo positionInfo = positionInfoRepository.findFirstByDispatchIdOrderByReportTimeDesc(dispatchId);
        ElectronicLockMessage electronicLockMessage = electronicLockMessageRepository.findFirstByDispatchIdOrderByIdDesc(dispatchId);
        return BaseDTO.createBaseDTO(electronicLockMessageMapper.toDto(electronicLockMessage));
    }

    /**
     * 查看历史轨迹-此次调度所有的位置信息
     * @param dispatchId
     * @return
     */
    public BaseDTO searchPositionInfoOld(String dispatchId){
        if(dispatchId==null || "".equals(dispatchId)){
            return new BaseDTO(BaseDTO.CODE_PARAM, "dipatch_info_id_not_null", null);
        }
//        List<PositionInfoDTO> positionInfoDTOS = positionInfoRepository.findAllByDispatchIdOrderByReportTime(dispatchId).stream().
//                map(positionInfoMapper::toDto).collect(Collectors.toList());

        List<ElectronicLockMessageDTO> electronicLockMessageDTOS = electronicLockMessageRepository.
                findAllByDispatchIdOrderByTimeAsc(dispatchId).stream().map( n -> {
                    return electronicLockMessageMapper.toDto(n);
        }).collect(Collectors.toList());

        return BaseDTO.createBaseDTO(electronicLockMessageDTOS);
    }



}
